﻿using Microsoft.EntityFrameworkCore.Infrastructure;
using Microsoft.Extensions.DependencyInjection;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using WebApiV3.Data;
using WebApiV3.Data.Entity;
using WebApiV3.Domain;

namespace WebApiV3.Utils
{
    public class DbInitializer
    {

        public static void Seed(IServiceProvider serviceProvider)
        {
            using (var scope = serviceProvider.CreateScope())
            {
                var db = scope.ServiceProvider.GetService(typeof(Admin1002Context)) as Admin1002Context;

                var _user = scope.ServiceProvider.GetService(typeof(IResponsitory<Users>)) as EfResponsitory<Users>;

                var _role = scope.ServiceProvider.GetService(typeof(IResponsitory<Roles>)) as EfResponsitory<Roles>;

                db.Database.EnsureCreated();

                var hasUser = db.Users.Any();
                if (!hasUser)
                {
                    var role = new Roles()
                    {
                        RoleName = "管理员"
                    };
                    _role.Insert(role);
                    //var user = new Users()
                    //{
                    //    UserName = "sa",
                    //    Password = "123456",
                    //    RolesId=role.Id
                    //};
                    _user.InsertBulk(new Users[] 
                    { 
                        new Users
                        {
                            UserName="root",
                            Password="1123",
                            RolesId=role.Id
                        },
                        new Users
                        {
                            UserName="sa",
                            Password="3321",
                            RolesId=role.Id
                        }
                    });
                }
            }
        }
        //using var db = new Admin3200DbContext();


    }

}

